package com.niiwoo.civet.trade.service.local.loan.antifraud;

import com.niiwoo.civet.trade.dao.entity.ProjectFlowExecuteRuntime;
import com.niiwoo.civet.trade.dao.entity.TianChengRequestRecord;
import com.niiwoo.civet.trade.dao.mapper.ProjectFlowExecuteRuntimeMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectMapperExt;
import com.niiwoo.civet.trade.dao.mapper.TianChengRequestRecordMapperExt;
import com.niiwoo.civet.trade.dto.request.loan.ProjectFlowExecuteRuntimeDTO;
import com.niiwoo.civet.trade.dto.request.loan.TianChengRequestRecordDTO;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lujing on 2018/4/18.
 */
@Slf4j
@Service
public class LoanAntiFraudLocalService {

    @Autowired
    private ProjectMapperExt projectMapperExt;
    @Autowired
    private TianChengRequestRecordMapperExt tianChengRequestRecordMapperExt;
    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    ProjectFlowExecuteRuntimeMapperExt projectFlowExecuteRuntimeMapperExt;
    /**
     * 查询用户是否为复借用户（除去极速借机构）
     * @param userId
     * @return
     */
    public Boolean firstLoan(String userId){
        List<Byte> statusList = new ArrayList<>();
        statusList.add(ProjectStatusEnum.REPAYING.getStatus());
        statusList.add(ProjectStatusEnum.FINISH.getStatus());
        Integer succTime =  projectMapperExt.querySuccProjectByStatus(userId,statusList);
        if(succTime != null && succTime > 0){
            return true;
        }
        return false;
    }

    /**
     * 增加天秤请求记录
     * @param tianChengRequestRecordDTO
     */
    @Transactional
    public void tianChengRequestRecord(TianChengRequestRecordDTO tianChengRequestRecordDTO){
        TianChengRequestRecord record = new TianChengRequestRecord();
        BeanUtils.copyProperties(tianChengRequestRecordDTO, record);
        record.setId(snowflakeIdWorker.nextId());
        record.setCreateTime(new Date());
        tianChengRequestRecordMapperExt.insertSelective(record);
    }

    public ProjectFlowExecuteRuntimeDTO queryById(Long flowExecuteId){
        ProjectFlowExecuteRuntimeDTO projectFlowExecuteRuntimeDTO = new ProjectFlowExecuteRuntimeDTO();
        ProjectFlowExecuteRuntime projectFlowExecuteRuntime = projectFlowExecuteRuntimeMapperExt.selectByPrimaryKey(flowExecuteId);
        if(projectFlowExecuteRuntime != null){
            BeanUtils.copyProperties(projectFlowExecuteRuntime,projectFlowExecuteRuntimeDTO);
        }
        return projectFlowExecuteRuntimeDTO;
    }
}
